﻿Imports System
Imports System.IO
Imports System.Data.OleDb
Imports System.Data
Namespace Arwani
    Partial Class Backend_ManageBrands
        Inherits System.Web.UI.Page

        Public Function getImage(ByVal imgSrc As String) As String
            Dim strReturn As String = "../images/main_banner.png"
            If File.Exists(Server.MapPath("../Brands/" & imgSrc)) Then
                strReturn = "../Brands/" & imgSrc
            End If
            Return strReturn
        End Function

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If (Session("ADMIN_USER") IsNot Nothing) Then
                If IsPostBack Then Exit Sub
                Dim strQ As String = "select * from division where divisionid= " & CInt(Request.QueryString("divid"))
                Dim arwCon As New ArwaniController
                Dim dtTable As DataTable = arwCon.ExecuteQuery(strQ)
                If dtTable.Rows.Count > 0 Then
                    If Not IsDBNull(dtTable.Rows(0)("divisiontitle")) Then Me.lblBrandDivHeading.Text = "Brands in Division : " & dtTable.Rows(0)("divisiontitle")
                End If
                Call loadData("")
                Call loadDivision()
            Else
                Response.Redirect("Default.aspx")
            End If

        End Sub

        Public Sub loadData(ByVal strFilter As String)
            Dim arwCon As New ArwaniController
            Dim strQ As String = ""
            If strFilter <> "" Then
                If Me.cboDivision.SelectedIndex > 0 Then
                    If Me.cboCategory.SelectedIndex > 0 Then
                        strQ = "SELECT distinct b.* FROM brand b, brand_division_category bdc where b.brandname like '%" & strFilter & "%' and b.brandid=bdc.brandid and bdc.divisionid=" & Me.cboDivision.SelectedItem.Value & " and bdc.categoryid=" & Me.cboCategory.SelectedItem.Value & "  order by b.sortorder"
                    Else
                        strQ = "SELECT distinct b.* FROM brand b, brand_division_category bdc where b.brandname like '%" & strFilter & "%' and b.brandid=bdc.brandid and bdc.divisionid=" & Me.cboDivision.SelectedItem.Value & " order by b.sortorder"
                    End If
                Else
                    strQ = "select distinct * from brand where brandname like '%" & strFilter & "%' order by brandname"
                End If

                '
            Else
                If Me.cboDivision.SelectedIndex > 0 Then
                    If Me.cboCategory.SelectedIndex > 0 Then
                        strQ = "SELECT distinct b.* FROM brand b, brand_division_category bdc where b.brandid=bdc.brandid and bdc.divisionid=" & Me.cboDivision.SelectedItem.Value & " and bdc.categoryid=" & Me.cboCategory.SelectedItem.Value & "  order by b.sortorder"
                    Else
                        strQ = "SELECT distinct b.* FROM brand b, brand_division_category bdc where b.brandid=bdc.brandid and bdc.divisionid=" & Me.cboDivision.SelectedItem.Value & " order by b.sortorder"
                    End If
                Else
                    strQ = "select distinct * from brand  order by brandname"
                End If
            End If
            '  Response.Write(strQ)

            Dim dtTable As DataTable = arwCon.ExecuteQuery(strQ)

            Me.dgBrands.DataSource = dtTable
            Me.dgBrands.DataBind()



        End Sub

        Protected Sub dgBrands_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgBrands.ItemCommand

            If e.CommandName = "Edit" Then
                Response.Redirect("EditBrand.aspx?bid=" & CInt(e.CommandArgument))
            ElseIf e.CommandName = "Manage" Then
                Response.Redirect("ManageBrandDivCat.aspx?bid=" & CInt(e.CommandArgument))
            ElseIf e.CommandName = "Delete" Then
                Dim arwCon As New ArwaniController
                Dim strQ As String = ""
                strQ = "select * from brand where brandid= " & CInt(e.CommandArgument)
                Dim dtTable As DataTable = arwCon.ExecuteQuery(strQ)
                If dtTable.Rows.Count > 0 Then
                    Try
                        If File.Exists(Server.MapPath("../Brands/" & dtTable.Rows(0)("brandlogo"))) Then
                            File.Delete(Server.MapPath("../Brands/" & dtTable.Rows(0)("brandlogo")))
                        End If
                        If File.Exists(Server.MapPath("../Brands/" & dtTable.Rows(0)("brandpic"))) Then
                            File.Delete(Server.MapPath("../Brands/Thumb_" & dtTable.Rows(0)("brandpic")))
                            File.Delete(Server.MapPath("../Brands/Thumb_small_" & dtTable.Rows(0)("brandpic")))
                        End If
                    Catch ex As Exception

                    End Try
                End If

                strQ = "delete from brand where brandid= " & CInt(e.CommandArgument)
                arwCon.ExecuteNonQuery(strQ)
                Call loadData(Me.txtSearch.Text)
            End If
        End Sub

        Public Function getCategoryName(ByVal catid As Integer) As String
            Dim arwCon As New ArwaniController
            Dim strQ As String = "select * from category where catid=" & catid
            Dim dtTable As DataTable = arwCon.ExecuteQuery(strQ)
            If dtTable.Rows.Count > 0 Then
                Return dtTable.Rows(0)("catname")
            End If
            Return "N/A"
        End Function

        Protected Sub dgBrands_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgBrands.ItemDataBound
            Try
                Dim lnkDelete As LinkButton = CType(e.Item.FindControl("lnkDelete"), LinkButton)
                lnkDelete.Attributes.Add("onclick", "return confirm('Confirm delete this brand?')")


                Dim brandID As Integer = dgBrands.DataKeys(e.Item.ItemIndex)
                Dim txtSortOrder As TextBox = CType(e.Item.FindControl("txtSortOrder"), TextBox)
                txtSortOrder.Attributes.Add("onblur", "postSortOrder(this.value,'brand', 'brandid','" & brandID & "')")
            Catch ex As Exception

            End Try
        End Sub
        Protected Sub dgBrands_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles dgBrands.PageIndexChanged
            Me.dgBrands.CurrentPageIndex = e.NewPageIndex
            Call loadData(Me.txtSearch.Text)
        End Sub
          
        Protected Sub lnkAddNewBrand_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkAddNewBrand.Click
            Response.Redirect("EditBrand.aspx?bid=0" & "&divid=" & CInt(Request.QueryString("divid")))
        End Sub

        Protected Sub lnkSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkSearch.Click
            Call loadData(Me.txtSearch.Text)
        End Sub

        Protected Sub lnkBackToDiv_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkBackToDiv.Click
            Response.Redirect("ManageDivision.aspx")
        End Sub
        Public Sub loadCategory(ByVal divID As Integer)

            Dim arwCon As New ArwaniController
            Dim strQ As String = "select * from category where divisionid=" & divID
            Dim dtTable As DataTable = arwCon.ExecuteQuery(strQ)
            If dtTable.Rows.Count > 0 Then
                Me.rdCategory.Visible = True
                Me.cboCategory.DataTextField = "catname"
                Me.cboCategory.DataValueField = "catid"
                Me.cboCategory.DataSource = dtTable
                Me.cboCategory.DataBind()
                Me.cboCategory.Items.Insert(0, New ListItem("Any Category", 0))
            Else
                Me.rdCategory.Visible = False
            End If
            

        End Sub
        Public Function getBrandDivCat(ByVal BrandID As Integer) As String
            Dim arwCon As New ArwaniController
            Dim strDivCat As String = ""
            Dim iCount As Integer = 0 
            Dim strQ As String = "SELECT d.divisiontitle,c.catname FROM category c,division d, brand_division_category bdc where d.divisionid=bdc.divisionid and c.catid=bdc.categoryid and bdc.brandid=" & BrandID
            Dim dtTable As DataTable = arwCon.ExecuteQuery(strQ)
            If dtTable.Rows.Count > 0 Then
                For iCount = 0 To dtTable.Rows.Count - 1
                    strDivCat &= dtTable.Rows(iCount)("divisiontitle") & "/" & dtTable.Rows(iCount)("catname") & ","
                Next
                If strDivCat <> "" Then strDivCat = strDivCat.Substring(0, strDivCat.Length - 1)
            End If
            Return strDivCat
        End Function
        Public Sub loadDivision()
            Dim arwCon As New ArwaniController
            Dim strQ As String = "select * from division "
            Dim dtTable As DataTable = arwCon.ExecuteQuery(strQ)

            Me.cboDivision.DataSource = dtTable
            Me.cboDivision.DataTextField = "divisiontitle"
            Me.cboDivision.DataValueField = "divisionid"

            Me.cboDivision.DataBind()
            Me.cboDivision.Items.Insert(0, New ListItem("Any Division", 0))
        End Sub

        Protected Sub cboDivision_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboDivision.SelectedIndexChanged
            If Me.cboDivision.SelectedIndex = -1 Then Exit Sub
            Call loadCategory(Me.cboDivision.SelectedItem.Value)
        End Sub
    End Class
End Namespace